﻿using System;
using System.Collections.Generic;
using System.Security.Permissions;
using System.Configuration;
using System.Web.Configuration;
using System.Web.UI.Design;
using System.ComponentModel.Design;
using System.Windows.Forms;

namespace Org.Eurekaa.DNJ.http
{
    public class DNJHttpHelper
    {

        public static void RegisterDNJHttpHandler(IWebApplication webApplication)
        {
            if (IsHttpHandlerRegistered(webApplication)) return;
            if (webApplication != null)
            {
                
                Configuration configuration = webApplication.OpenWebConfiguration(false);
                if (configuration != null)
                {
                    HttpHandlersSection handlersSection =
                        (HttpHandlersSection)configuration.GetSection(
                        "system.web/httpHandlers");
                    if (handlersSection == null)
                    {
                        handlersSection = new HttpHandlersSection();
                        ConfigurationSectionGroup group =
                            configuration.GetSectionGroup("system.web");
                        if (group == null)
                        {
                            configuration.SectionGroups.Add("system.web",
                                new ConfigurationSectionGroup());
                        }
                        group.Sections.Add("httpHandlers", handlersSection);
                    }
                    handlersSection.Handlers.Add(DNJHandlerAction);
                    configuration.Save(ConfigurationSaveMode.Minimal);
                }
            }
        }

        public static void RegisterDNJHttpModule(IWebApplication webApplication)
        {
            if (IsHttpModuleRegistered(webApplication)) return;
            if (webApplication != null)
            {
                Configuration configuration = webApplication.OpenWebConfiguration(false);
                if (configuration != null)
                {
                    HttpModulesSection modulesSection =
                        (HttpModulesSection)configuration.GetSection(
                        "system.web/httpModules");
                    if (modulesSection == null)
                    {
                        modulesSection = new HttpModulesSection();
                        ConfigurationSectionGroup group =
                            configuration.GetSectionGroup("system.web");
                        if (group == null)
                        {
                            configuration.SectionGroups.Add("system.web",
                                new ConfigurationSectionGroup());
                        }
                        group.Sections.Add("httpModules", modulesSection);
                    }
                    modulesSection.Modules.Add(DNJModuleAction);
                    configuration.Save(ConfigurationSaveMode.Minimal);
                }
            }
        }


        public static bool IsHttpHandlerRegistered(IWebApplication webApplication)
        {
            if (webApplication != null)
            {
                Configuration configuration =
                    webApplication.OpenWebConfiguration(true);

                if (configuration != null)
                {
                    HttpHandlersSection section =
                        (HttpHandlersSection)configuration.GetSection(
                        "system.web/httpHandlers");

                    if ((section != null) && (section.Handlers.IndexOf(DNJHandlerAction) >= 0))
                        return true;
                }
            }
            return false;
        }

        public static bool IsHttpModuleRegistered(IWebApplication webApplication)
        {

            if (webApplication != null)
            {
                Configuration configuration =
                    webApplication.OpenWebConfiguration(true);

                if (configuration != null)
                {
                    HttpModulesSection section =
                        (HttpModulesSection)configuration.GetSection(
                        "system.web/httpModules");

                    if ((section != null) && (section.Modules.IndexOf(DNJModuleAction) >= 0))
                        return true;
                }
            }
            return false;
        }

        private static HttpModuleAction DNJModuleAction
        {
            get
            {
                return new HttpModuleAction("DNJHttpModule", "Org.Eurekaa.DNJ.Http.DNJHttpModule,Org.Eurekaa.DNJ");
            }
        }
        private static HttpHandlerAction DNJHandlerAction
        {
            get
            {                
                return new HttpHandlerAction(
                    "DNJResources.axd",
                    "Org.Eurekaa.DNJ.http.DNJHttpHandler, Org.Eurekaa.DNJ",
                    "*"
                );
            }
        }

        public static void RegisterInComponent(IWebApplication webApplication, bool httpHandler, bool httpModule)
        {
            bool NeedRegistration = true;            

            NeedRegistration = !((IsHttpHandlerRegistered(webApplication) || !httpHandler)
                && (IsHttpModuleRegistered(webApplication) || !httpModule));

            if (NeedRegistration)
            {
                if (MessageBox.Show(
                "Do you want to automatically register DNJ Modules needed by this control in the web.config?",
                "Confirmation", MessageBoxButtons.YesNo,
                MessageBoxIcon.Exclamation) == DialogResult.Yes)
                {
                    RegisterDNJHttpHandler(webApplication);
                    RegisterDNJHttpModule(webApplication);
                }
                else
                {
                    MessageBox.Show("You didn't registered DNJ Modules, you need to register them manually otherwise, you will not be able to use RPC, ajax and advanced UI features",
                        "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }

        }
        
        //public override string GetDesignTimeHtml(DesignerRegionCollection regions)
        //{
        //    if (!IsHttpHandlerRegistered() &&
        //        (MessageBox.Show(
        //        "Do you want to automatically register the HttpHandler needed by this control in the web.config?",
        //        "Confirmation", MessageBoxButtons.YesNo,
        //        MessageBoxIcon.Exclamation) == DialogResult.Yes))
        //        RegisterImageHttpHandler();
        //    return base.CreatePlaceHolderDesignTimeHtml("Word 2003 XML Viewer");
        //}


    }
}
